1
|
|
|
var Conclusion = require('./Conclusion'), |
2
|
|
|
Identifiers = require('./Identifiers'), |
3
|
|
|
EvidenceReference = require('./EvidenceReference'), |
4
|
|
|
SourceReference = require('./SourceReference'), |
5
|
|
|
utils = require('./utils'); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An object identified in time and space by various conclusions. |
9
|
|
|
* |
10
|
|
|
* @constructor |
11
|
|
|
* @param {Object} [json] |
|
|
|
|
12
|
|
|
*/ |
13
|
|
|
var Subject = function(json){ |
14
|
|
|
|
15
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
16
|
|
|
if(!(this instanceof Subject)){ |
17
|
|
|
return new Subject(json); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
21
|
|
|
if(Subject.isInstance(json)){ |
22
|
|
|
return json; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
Conclusion.call(this, json); |
26
|
|
|
|
27
|
|
|
if(json){ |
|
|
|
|
28
|
|
|
// setExtracted defaults to false but when the property is undefined we |
29
|
|
|
// want it to stay that way |
30
|
|
|
if(typeof json.extracted !== 'undefined'){ |
31
|
|
|
this.setExtracted(json.extracted); |
32
|
|
|
} |
33
|
|
|
this.setEvidence(json.evidence); |
34
|
|
|
this.setIdentifiers(json.identifiers); |
35
|
|
|
this.setMedia(json.media); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
Subject.prototype = Object.create(Conclusion.prototype); |
40
|
|
|
|
41
|
|
|
Subject._gedxClass = Subject.prototype._gedxClass = 'GedcomX.Subject'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check whether the given object is an instance of this class. |
45
|
|
|
* |
46
|
|
|
* @param {Object} obj |
47
|
|
|
* @returns {Boolean} |
48
|
|
|
*/ |
49
|
|
|
Subject.isInstance = function(obj){ |
50
|
|
|
return utils.isInstance(obj, this._gedxClass); |
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Is this an extracted conclusion? |
55
|
|
|
* |
56
|
|
|
* @returns {Boolean} extracted |
57
|
|
|
*/ |
58
|
|
|
Subject.prototype.isExtracted = function(){ |
59
|
|
|
// Double exclamations force a boolean no matter what type the property |
60
|
|
|
// currently is. The main reason for this is to force undefiend into false. |
61
|
|
|
return !!this.isExtracted; |
62
|
|
|
}; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the extracted property |
66
|
|
|
* |
67
|
|
|
* @param {Boolean} extracted |
68
|
|
|
* @returns {Subject} This instance. |
69
|
|
|
*/ |
70
|
|
|
Subject.prototype.setExtracted = function(extracted){ |
71
|
|
|
// Double exclamations force a boolean |
72
|
|
|
this.extracted = !!extracted; |
73
|
|
|
return this; |
74
|
|
|
}; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the evidence. |
78
|
|
|
* |
79
|
|
|
* @returns {EvidenceReference[]} |
80
|
|
|
*/ |
81
|
|
|
Subject.prototype.getEvidence = function(){ |
82
|
|
|
return this.evidence || []; |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the evidence. This method will replace existing evidence entries with new entries. |
87
|
|
|
* |
88
|
|
|
* @param {Object[]|EvidenceReference[]} |
|
|
|
|
89
|
|
|
* @returns {Subject} This instance. |
90
|
|
|
*/ |
91
|
|
|
Subject.prototype.setEvidence = function(evidence){ |
92
|
|
|
return this._setArray(evidence, 'evidence', 'addEvidence'); |
93
|
|
|
}; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add evidence |
97
|
|
|
* |
98
|
|
|
* @param {Object|EvidenceReference} |
|
|
|
|
99
|
|
|
* @returns {Subject} This instance. |
100
|
|
|
*/ |
101
|
|
|
Subject.prototype.addEvidence = function(evidence){ |
102
|
|
|
return this._arrayPush(evidence, 'evidence', EvidenceReference); |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the identifiers |
107
|
|
|
* |
108
|
|
|
* @return {Identifiers} |
109
|
|
|
*/ |
110
|
|
|
Subject.prototype.getIdentifiers = function(){ |
111
|
|
|
return this.identifiers; |
112
|
|
|
}; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the identifiers |
116
|
|
|
* |
117
|
|
|
* @param {Object|Identifiers} |
|
|
|
|
118
|
|
|
* @returns {Subject} This instance |
119
|
|
|
*/ |
120
|
|
|
Subject.prototype.setIdentifiers = function(identifiers){ |
121
|
|
|
if(identifiers){ |
122
|
|
|
this.identifiers = Identifiers(identifiers); |
123
|
|
|
} |
124
|
|
|
return this; |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the media references |
129
|
|
|
* |
130
|
|
|
* @returns {SourceReference[]} |
131
|
|
|
*/ |
132
|
|
|
Subject.prototype.getMedia = function(){ |
133
|
|
|
return this.media || []; |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the media references |
138
|
|
|
* |
139
|
|
|
* @param {Object[]|SourceReference[]} |
140
|
|
|
*/ |
141
|
|
|
Subject.prototype.setMedia = function(media){ |
142
|
|
|
return this._setArray(media, 'media', 'addMedia'); |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add media |
147
|
|
|
* |
148
|
|
|
* @param {Object|SourceReference} |
|
|
|
|
149
|
|
|
* @returns {Subject} This instance. |
150
|
|
|
*/ |
151
|
|
|
Subject.prototype.addMedia = function(media){ |
152
|
|
|
return this._arrayPush(media, 'media', SourceReference); |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Export the object as JSON |
157
|
|
|
* |
158
|
|
|
* @return {Object} JSON object |
159
|
|
|
*/ |
160
|
|
|
Subject.prototype.toJSON = function(){ |
161
|
|
|
return this._toJSON(Conclusion, [ |
162
|
|
|
'extracted', |
163
|
|
|
'evidence', |
164
|
|
|
'identifiers', |
165
|
|
|
'media' |
166
|
|
|
]); |
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
module.exports = Subject; |